package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.ServiceListEntiy;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.AddressHelper;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ServiceViewModel extends AndroidViewModel {


    private int pageNum = 1;
    IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");


    public MutableLiveData<Integer> show_Loding = new MutableLiveData<>();
    public MutableLiveData<List<IpPickSelectEntity>> showpop_list = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> showpop_address = new MutableLiveData<>();
    public MutableLiveData<String> show_toast = new MutableLiveData<>();
    public MutableLiveData<List<ServiceListEntiy>> listdata = new MutableLiveData<>();
    public boolean isLoadMore = false;



    public ServiceViewModel(@NonNull @NotNull Application application) {
        super(application);
    }




    public void getProfessQuoteList(boolean isLoadMore, Map map) {
        if (!isLoadMore) {
            show_Loding.postValue(1);
        }

        this.isLoadMore = isLoadMore;
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("limit", 20);
        map.put("pageSize", pageNum);
        map.put("asc",false);
        map.put("sortType",1);

        show_Loding.postValue(0);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceHallCompanyOrTeamList(map).enqueue(new Callback<BaseEntity<List<ServiceListEntiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ServiceListEntiy>>> call, Response<BaseEntity<List<ServiceListEntiy>>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    listdata.postValue(response.body().getData());
                    show_Loding.postValue(2);
                } else {
                    show_toast.postValue(response.body().getMsg());
                    show_Loding.postValue(3);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ServiceListEntiy>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 获取服务城市
     */
    public void getServiceCity() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.CallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                if (data != null && data.size() > 0) {
//                    fragment.showCityPopWindow2(data);
                    showpop_address.postValue(data);
                }
            }

            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {

            }
        });
    }

    public void getPriceOptionByNo(int priceServiceNo) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPriceOption(priceServiceNo).enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
//                    fragment.showMakeupPopWindow(list1);
                    showpop_list.postValue(list1);
                } else {
                    show_toast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }



    public void setFollow(String no,int type){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("businessNo",no);
        hashMap.put("type",type);
//        show_Loding.postValue(0);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().setFollowForCompanyAndTeam(hashMap).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                DialogHelper.dismissProgressDialog();

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    show_Loding.postValue(4);

                } else {
                    show_toast.postValue(response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
